import {messages} from "share/common";
import React from 'react'
import httpFetch from 'share/httpFetch';

import config from 'config'
import { connect } from 'react-redux'


import { Alert, Form, Switch, Icon, Input, Select, Button, Row, Col, message } from 'antd'
const FormItem = Form.Item;
const Option = Select.Option;


import budgetOrganizationService from 'containers/budget-setting/budget-organization/budget-organnization.service'
import 'styles/budget-setting/budget-organization/new-budget-organization.scss'

class UpdateBudgetParameterSetting extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      loading: false,
      statusOptions:[],
    };
  }

  componentWillMount() {
    if(this.props.params.parameterId){
      this.getStatusOptions();
    }
  }
  getStatusOptions(){
    httpFetch.get(`${config.budgetUrl}/api/budget/parameterSettings/parameterValues/${this.props.params.parameterId}`).then(res => { //状态
      let statusOptions = res.data || [];
      this.setState({
        statusOptions
      })
    })
  }

  onCancel = () => {
    this.props.close();
  };

  handleSave = (e) => {
    e.preventDefault();
    this.props.form.validateFieldsAndScroll((err, values) => {
      if (!err) {
        this.setState({loading: true});
        let params = {
          id: this.props.params.id,
          isEnabled: values.isEnabled,
          parameterCode: this.props.params.parameterCode,
          parameterValueCode: values.parameterValueCode,
          tenantId: this.props.params.tenantId,
          setOfBooksId: this.props.params.setOfBooksId,
          versionNumber: this.props.params.versionNumber
        };
        budgetOrganizationService.updateParameterSetting(params).then((res)=>{
          this.setState({loading: false});
          message.success(messages('common.save.success', {name: res.data.parameterName}));  //保存成功
          this.props.close(true);
        }).catch((e)=>{
          if(e.response){
            message.error(`${messages('common.save.filed')}, ${e.response.data.message}`);
          }
          this.setState({loading: false});
        })
      }
    });
  };

  render(){
    const { getFieldDecorator } = this.props.form;
    const {statusOptions} = this.state;
    const formItemLayout = {
      labelCol: { span: 8 },
      wrapperCol: { span: 10, offset: 1 },
    };
    return (
      <div className="new-budget-organization">
        <Form onSubmit={this.handleSave}>
          <FormItem {...formItemLayout} label={messages('budget-setting.key127'/*参数代码*/)}>
            {getFieldDecorator('parameterCode', {
              rules: [{
                required: true
              }],
              initialValue: this.props.params.parameterCode
            })(
              <Input disabled/>
            )}
          </FormItem>
          <FormItem {...formItemLayout} label={messages('budget-setting.key128'/*参数名称*/)}>
            {getFieldDecorator('parameterName', {
              rules: [{
                required: true
              }],
              initialValue: this.props.params.parameterName
            })(
              <Input disabled/>
            )}
          </FormItem>
          <FormItem {...formItemLayout} label={messages('budget-setting.key130'/*参数值名称*/)}>
            {getFieldDecorator('parameterValueCode', {
              rules: [{
                required: true,
                message: messages('common.please.enter'),  //请输入
              }],
              initialValue: this.props.params.parameterValueCode
            })(
              <Select placeholder={messages('common.please.select')} getPopupContainer={triggerNode => triggerNode.parentNode}>
                  {statusOptions.map((option) => {
                    return <Option key={option.parameterValueCode}>{option.parameterValueName}</Option>
                  })}
                </Select>
            )}
          </FormItem>

          <div className="slide-footer">
            <Button type="primary" htmlType="submit" loading={this.state.loading}>{messages('common.save')/* 保存 */}</Button>
            <Button onClick={this.onCancel}>{messages('common.cancel')/* 取消 */}</Button>
          </div>
        </Form>
      </div>
    )
  }

}

function mapStateToProps() {
  return {}
}
const WrappedUpdateBudgetParameterSetting = Form.create()(UpdateBudgetParameterSetting);

export default connect(mapStateToProps)(WrappedUpdateBudgetParameterSetting);
